﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class passrecover : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnresetpassword_Click(object sender, EventArgs e)
    {
        if (UserAuthenticate(txtemail.Text))
        {
            MailMessage mail = new MailMessage();
            mail.Subject = "reset mail";
            mail.Body = "your pass: 123456";
            //mail.Sender =  new MailAddress("admin@cnn.com");

            //
            mail.From = new MailAddress("admin@cnn.com");
            mail.To.Add(new MailAddress(txtemail.Text));
            SmtpClient smtp = new SmtpClient("localhost");
            smtp.Send(mail);

            Label1.Text = "new password sent to your email";
            email.Enabled = false;
            txtemail.Enabled = false;
            btnresetpassword.Enabled = false;

        }
        else {
            Label notice = new Label();
            notice.Text = " <br />wrong email!!!!!";
            notice.Attributes.Add("style","color:red");
            pass_recover.Controls.Add(notice);
        }
        
    }

    //check whether exist an account which provided by user
    private bool UserAuthenticate(string UserName)
    {
        DatabaseProcess dp = new DatabaseProcess();
        bool boolReturnValue = false;
        DataTable dt = new DataTable();
        string chkUser = "Select * FROM [User_] where Email='" + UserName + "'"; 
        dt = dp.ConnectDataBaseReturnDT(chkUser);
        if (dt.Rows.Count > 0)
        {
            boolReturnValue = true;
        }
        return boolReturnValue;
    }
}